home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xgrabsc / xgrabxaw.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  15KB  |  590 lines

  1. /*========================================================================
  2.  *
  3.  * Name - xgrabxaw.c
  4.  *
  5.  * Version:    1.16
  6.  *
  7.  * ccsid:    @(#)xgrabxaw.c    1.16 - 06/28/93 09:13:54
  8.  * from:     ccs/s.xgrabxaw.c
  9.  * date:     06/28/93 09:14:49
  10.  *
  11.  * Copyright (c) 1990-93 Bruce Schuchardt.
  12.  * Read the file cpyright.h for full copyright information.
  13.  *
  14.  *
  15.  * Description:
  16.  *
  17.  * xgrab.c - interactive front for xgrabsc
  18.  *
  19.  *========================================================================
  20.  */
  21. #include "cpyright.h"
  22. #include "patchlevel.h"
  23. #include "config.h"
  24.  
  25. #include <stdio.h>
  26. #include <X11/Intrinsic.h>
  27. #include <X11/StringDefs.h>
  28. #include <X11/Shell.h>
  29. #include <X11/Xaw/Form.h>
  30. #include <X11/Xaw/Label.h>
  31. #include <X11/Xaw/Command.h>
  32. #include <X11/Xaw/AsciiText.h>
  33. #include <X11/Xaw/Toggle.h>
  34.  
  35. static char *fallback_resources[] = {
  36. #include "xgrabxaw_ad.h"
  37.   NULL
  38.   };
  39.  
  40.  
  41. static Display *hDisplay;
  42.  
  43. typedef enum {
  44.   HITWIN = 1,
  45.   STRETCH,
  46.   KEY,
  47.   ROOT,
  48.  
  49.   XWD,
  50.   XWDXY,
  51.   PS,
  52.   BITMAP,
  53.   BITMAP2,
  54.   BITMAP3,
  55.   PUZZLE,
  56.   SIMPLE,
  57.  
  58.   DITHER,
  59.   MDITHER,
  60.   HALFTONE,
  61.   MAPBW,
  62.  
  63.   REVERSE,
  64.  
  65.   TOFILE,
  66.   TOPRN,
  67.  
  68.   PS_NORMAL,
  69.   PS_EPS,
  70.   PS_EPSI,
  71.   PS_EPSIONLY,
  72.   PS_LANDSCAPE
  73.   
  74.   } radioType;
  75.  
  76.  
  77. Widget selBtns, formatBtns, psColr, encBtns, psComp, sltext, psltext,
  78.        psLim,
  79.        psBin, borderBtn, horizMargin, vertMargin, pgHeight, pgWidth,
  80.        cnvBtns, revBtn, brtext, outBtns, filenm, prncmd, dispBtn, hostnm;
  81. Widget hShell;
  82.  
  83. XtAppContext appContext;
  84.  
  85.  
  86. static char* stradi(ptr, i)    /* Add an integer to a string */
  87.     char* ptr;
  88.     int i; {
  89.  
  90.     sprintf(ptr + strlen(ptr), "%d", i);
  91.     return ptr;
  92. }
  93.  
  94. static char* stradw(ptr, s)   /* Add a string to another, with no ws */
  95.     char* ptr;
  96.     char* s; {
  97.  
  98.     char* ou = ptr + strlen(ptr);
  99.  
  100.     while (isspace(*s)) {
  101.     ++s;
  102.     }
  103.     while (*s && !isspace(*s)) {
  104.     *ou++ = *s++;
  105.     }
  106.     *ou = '\0';
  107.  
  108.     return ptr;
  109. }
  110.  
  111.  
  112. static void doGrab() {
  113.   char tmp[257];
  114.   char cmdargs[200];
  115.   char piper[200];
  116.   char cmd[500];
  117.   char *txt;
  118.   int  s, d, len, num;
  119.   int  sltime, psltime;
  120.   int  bright;
  121.   Boolean state;
  122.  
  123.   cmdargs[0] = '\0';
  124.   piper[0] = '\0';
  125.  
  126.   XtVaGetValues(sltext, XtNstring, &txt, NULL);
  127.   sltime = atoi(txt);
  128.   if (sltime < 0) sltime = 0;
  129.   sprintf(tmp, "-sleep %d ", sltime);
  130.   strcat(cmdargs, tmp);
  131.  
  132.   XtVaGetValues(psltext, XtNstring, &txt, NULL);
  133.   psltime = atoi(txt);
  134.   if (psltime < 0) psltime = 0;
  135.   sprintf(tmp, "-post %d ", psltime);
  136.   strcat(cmdargs, tmp);
  137.  
  138.   XtVaGetValues(brtext, XtNstring, &txt, NULL);
  139.   bright = atoi(txt);
  140.   sprintf(tmp, "-brighten %d ", bright);
  141.   strcat(cmdargs, tmp);
  142.  
  143.   XtVaGetValues(borderBtn, XtNstate, &state, NULL);
  144.   if (!state)
  145.     strcat(cmdargs, "-");
  146.   strcat(cmdargs, "-bdrs ");
  147.  
  148.   XtVaGetValues(revBtn, XtNstate, &state, NULL);
  149.   if (!state)
  150.     strcat(cmdargs, "-");
  151.   strcat(cmdargs, "-reverse ");
  152.  
  153.   switch ((int)XawToggleGetCurrent(selBtns)) {
  154.     case HITWIN:
  155.       strcat(cmdargs, "-click ");
  156.       break;
  157.     case KEY:
  158.       strcat(cmdargs, "-key ");
  159.       break;
  160.     case ROOT:
  161.       strcat(cmdargs, "-root ");
  162.       break;
  163.     case STRETCH:
  164.     default:
  165.       strcat(cmdargs, "-stretch ");
  166.       break;
  167.   }
  168.   switch ((int)XawToggleGetCurrent(formatBtns)) {
  169.     case XWD:
  170.       strcat(cmdargs, "-xwd ");
  171.       break;
  172.     case XWDXY:
  173.       strcat(cmdargs, "-xwdxy ");
  174.       break;
  175.     case BITMAP:
  176.       strcat(cmdargs, "-bm ");
  177.       break;
  178.     case BITMAP2:
  179.       strcat(cmdargs, "-bm2 ");
  180.       break;
  181.     case BITMAP3:
  182.       strcat(cmdargs, "-bm3 ");
  183.       break;
  184.     case PUZZLE:
  185.       strcat(cmdargs, "-puzzle ");
  186.       break;
  187.     case SIMPLE:
  188.       strcat(cmdargs, "-simple ");
  189.       break;
  190.     case PS:
  191.     default:
  192.       XtVaGetValues(psColr, XtNstate, &state, NULL);
  193.       if (!state)
  194.         strcat(cmdargs, "-ps ");
  195.       else
  196.         strcat(cmdargs, "-cps ");
  197.  
  198.       switch ((int)XawToggleGetCurrent(encBtns)) {
  199.         case PS_EPS:
  200.           strcat(cmdargs, "-eps ");
  201.       break;
  202.     case PS_EPSI:
  203.       strcat(cmdargs, "-preview ");
  204.       break;
  205.     case PS_EPSIONLY:
  206.       strcat(cmdargs, "-previewonly ");
  207.       break;
  208.     case PS_LANDSCAPE:
  209.       strcat(cmdargs, "-landscape ");
  210.       break;
  211.     default:
  212.       break;
  213.       }
  214.  
  215.       XtVaGetValues(psComp, XtNstate, &state, NULL);
  216.       if (!state)
  217.         strcat(cmdargs, "-");
  218.       strcat(cmdargs, "-compress ");
  219.       XtVaGetValues(psBin, XtNstate, &state, NULL);
  220.       if (!state)
  221.         strcat(cmdargs, "-");
  222.       strcat(cmdargs, "-bin ");
  223.  
  224.       XtVaGetValues(psLim, XtNstate, &state, NULL);
  225.       if (!state)
  226.         strcat(cmdargs, "-");
  227.       strcat(cmdargs, "-limit ");
  228.  
  229.       strcat(cmdargs, "-page ");
  230.       XtVaGetValues(pgWidth, XtNstring, &txt, NULL);
  231.       if (strlen(txt))    stradw(cmdargs, txt);
  232.       else                stradi(cmdargs, PAPER_WIDTH);
  233.       strcat(cmdargs, "x");
  234.       XtVaGetValues(pgHeight, XtNstring, &txt, NULL);
  235.       if (strlen(txt))    stradw(cmdargs, txt);
  236.       else                stradi(cmdargs, PAPER_HEIGHT);
  237.       strcat(cmdargs, "-");
  238.       XtVaGetValues(horizMargin, XtNstring, &txt, NULL);
  239.       if (strlen(txt))    stradw(cmdargs, txt);
  240.       else                stradi(cmdargs, HORIZ_MARGIN);
  241.       strcat(cmdargs, "-");
  242.       XtVaGetValues(horizMargin, XtNstring, &txt, NULL);
  243.       if (strlen(txt))    stradw(cmdargs, txt);
  244.       else                stradi(cmdargs, VERT_MARGIN);
  245.       strcat(cmdargs, " ");
  246.  
  247.       break;
  248.   }
  249.   switch ((int)XawToggleGetCurrent(cnvBtns)) {
  250.     case DITHER:
  251.       strcat(cmdargs, "-dither ");
  252.       break;
  253.     case MDITHER:
  254.       strcat(cmdargs, "-mdither ");
  255.       break;
  256.     case HALFTONE:
  257.       strcat(cmdargs, "-halftone ");
  258.       break;
  259.     case MAPBW:
  260.       strcat(cmdargs, "-bw ");
  261.       break;
  262.     default:
  263.       break;
  264.   }
  265.   switch ((int)XawToggleGetCurrent(outBtns)) {
  266.     case TOPRN:
  267.       XtVaGetValues(prncmd, XtNstring, &txt, NULL);
  268.       if (strlen(txt)) {
  269.         strcat(piper, " | ");
  270.         strcat(piper, txt);
  271.       }
  272.       break;
  273.     case TOFILE:
  274.     default:
  275.       strcat(cmdargs, "-o ");
  276.       XtVaGetValues(filenm, XtNstring, &txt, NULL);
  277.       if (strlen(txt)) {
  278.         strcat(cmdargs, txt);
  279. #ifdef BUMP_FILENAMES
  280.     len = strlen(txt);
  281.     for (s=0; s<len && (txt[s] != '.'); s++)
  282.     {}
  283.     strcpy(tmp, txt);
  284.     tmp[s] = '\0';
  285.     d = s;
  286.     while (d > 0  &&  ('0' <= txt[d-1]  &&  txt[d-1] <= '9'))
  287.       d--;
  288.     if ('0' <= txt[d]  && txt[d] <= '9')
  289.       num = atoi(&tmp[d]);
  290.     else
  291.       num = 0;
  292.     num++;
  293.     sprintf(&tmp[d], "%d", num);
  294.     strcat(tmp, &txt[s]);
  295.     XtVaSetValues(filenm, XtNstring, tmp, NULL);
  296. #endif
  297.       }
  298.       else {
  299.         strcat(cmdargs, "screen.dmp");
  300.     XtVaSetValues(filenm, XtNstring, "screen.dmp", NULL);
  301.       }
  302.       strcat(cmdargs, " ");
  303.       break;
  304.   }
  305.  
  306.   XtVaGetValues(dispBtn, XtNstate, &state, NULL);
  307.   if (state) {
  308.     XtVaGetValues(hostnm, XtNstring, &txt, NULL);
  309.     strcat(cmdargs, "-d ");
  310.     if (strlen(txt)) {
  311.       strcat(cmdargs, txt);
  312.       strcat(cmdargs, " ");
  313.     }
  314.     else
  315.       strcat(cmdargs, ":0 ");
  316.   }
  317.  
  318.   sprintf(cmd, "xgrabsc %s%s\n", cmdargs, piper);
  319.   /* fputs(cmd, stderr); */
  320.   /* try to make the window iconic */
  321.   XtUnmapWidget(hShell);
  322.   XSync(hDisplay, False);
  323.   system(cmd);
  324.   XtMapWidget(hShell);
  325. }
  326.  
  327.  
  328.  
  329.  
  330. static void doDismiss() {
  331.   if (hShell) {
  332.     XtDestroyWidget(hShell);
  333.     hShell = NULL;
  334.     exit(0);
  335.   }
  336. }
  337.  
  338.  
  339.  
  340.  
  341. static void createWindow() {
  342.   Widget dialog, button, box1, box2, box3, box4, box5, box6;
  343.  
  344.   dialog = XtVaCreateManagedWidget("dialog", formWidgetClass, hShell,
  345.            NULL);
  346.            XtVaCreateManagedWidget("title",  labelWidgetClass, dialog,
  347.            NULL);
  348.  
  349.  
  350.   /* selection options */
  351.   /* input options */
  352.   box1 = XtVaCreateManagedWidget("box1", formWidgetClass, dialog, NULL);
  353.  
  354.            XtVaCreateManagedWidget("inputLbl", labelWidgetClass, box1,
  355.        NULL);
  356.  
  357.   dispBtn= XtVaCreateManagedWidget("host", toggleWidgetClass, box1,
  358.        NULL);
  359.  
  360.   hostnm = XtVaCreateManagedWidget("hostText", asciiTextWidgetClass, box1,
  361.            XtNeditType, XawtextEdit, NULL);
  362.  
  363.   if (getenv("DISPLAY"))
  364.     XtVaSetValues(hostnm, XtNstring, getenv("DISPLAY"), NULL);
  365.  
  366.   selBtns =
  367.            XtVaCreateManagedWidget("click", toggleWidgetClass, box1,
  368.            XtNradioData, HITWIN,
  369.        NULL);
  370.  
  371.            XtVaCreateManagedWidget("stretch", toggleWidgetClass, box1,
  372.            XtNradioData, STRETCH,
  373.        XtNradioGroup, selBtns,
  374.        NULL);
  375.  
  376.            XtVaCreateManagedWidget("key", toggleWidgetClass, box1,
  377.            XtNradioData, KEY,
  378.        XtNradioGroup, selBtns,
  379.        NULL);
  380.  
  381.            XtVaCreateManagedWidget("root", toggleWidgetClass, box1,
  382.            XtNradioData, ROOT,
  383.        XtNradioGroup, selBtns,
  384.        NULL);
  385.  
  386.            XtVaCreateManagedWidget("sleeplbl", labelWidgetClass, box1,
  387.            NULL);
  388.  
  389.   sltext = XtVaCreateManagedWidget("sleeptime", asciiTextWidgetClass, box1,
  390.            XtNeditType, XawtextEdit, NULL);
  391.  
  392.            XtVaCreateManagedWidget("psleeplbl", labelWidgetClass, box1,
  393.            NULL);
  394.  
  395.   psltext = XtVaCreateManagedWidget("psleeptime", asciiTextWidgetClass, box1,
  396.            XtNeditType, XawtextEdit, NULL);
  397.  
  398.   box2 = XtVaCreateManagedWidget("box2", formWidgetClass, dialog, NULL);
  399.  
  400.   /* output options */
  401.            XtVaCreateManagedWidget("outputFormat", labelWidgetClass, box2,
  402.            NULL);
  403.  
  404.   formatBtns =
  405.            XtVaCreateManagedWidget("xwd", toggleWidgetClass, box2,
  406.            XtNradioData, XWD,
  407.        NULL);
  408.  
  409.            XtVaCreateManagedWidget("ps", toggleWidgetClass, box2,
  410.            XtNradioData, PS,
  411.        XtNradioGroup, formatBtns,
  412.        NULL);
  413.  
  414.            XtVaCreateManagedWidget("puzzle", toggleWidgetClass, box2,
  415.            XtNradioData, PUZZLE,
  416.        XtNradioGroup, formatBtns,
  417.        NULL);
  418.  
  419.  
  420.            XtVaCreateManagedWidget("xyxwd", toggleWidgetClass, box2,
  421.            XtNradioData, XWDXY,
  422.        XtNradioGroup, formatBtns,
  423.        NULL);
  424.  
  425.            XtVaCreateManagedWidget("xpm", toggleWidgetClass, box2,
  426.            XtNradioData, BITMAP,
  427.        XtNradioGroup, formatBtns,
  428.        NULL);
  429.  
  430.            XtVaCreateManagedWidget("xpm2", toggleWidgetClass, box2,
  431.            XtNradioData, BITMAP2,
  432.        XtNradioGroup, formatBtns,
  433.        NULL);
  434.  
  435.            XtVaCreateManagedWidget("xpm3", toggleWidgetClass, box2,
  436.            XtNradioData, BITMAP3,
  437.        XtNradioGroup, formatBtns,
  438.        NULL);
  439.  
  440.  
  441.   /* postscript options */
  442.   box3 = XtVaCreateManagedWidget("box3", formWidgetClass, dialog, NULL);
  443.  
  444.            XtVaCreateManagedWidget("psOptions", labelWidgetClass, box3,
  445.            NULL);
  446.  
  447.   psComp = XtVaCreateManagedWidget("compress", toggleWidgetClass, box3,
  448.        NULL);
  449.  
  450.   psColr = XtVaCreateManagedWidget("color", toggleWidgetClass, box3,
  451.        NULL);
  452.  
  453.   psBin = XtVaCreateManagedWidget("binary", toggleWidgetClass, box3,
  454.        NULL);
  455.  
  456.   psLim = XtVaCreateManagedWidget("limit", toggleWidgetClass, box3,
  457.        NULL);
  458.  
  459.   encBtns= XtVaCreateManagedWidget("landscape", toggleWidgetClass, box3,
  460.            XtNradioData, PS_LANDSCAPE,
  461.        NULL);
  462.  
  463.            XtVaCreateManagedWidget("epsi", toggleWidgetClass, box3,
  464.            XtNradioData, PS_EPSI,
  465.        XtNradioGroup, encBtns,
  466.        NULL);
  467.  
  468.            XtVaCreateManagedWidget("onlyEpsi", toggleWidgetClass, box3,
  469.            XtNradioData, PS_EPSIONLY,
  470.        XtNradioGroup, encBtns,
  471.        NULL);
  472.  
  473. /*------------
  474.            XtVaCreateManagedWidget("encap", toggleWidgetClass, box3,
  475.            XtNradioData, PS_EPS,
  476.        XtNradioGroup, encBtns,
  477.        NULL);
  478. --------------*/
  479.  
  480.  
  481.            XtVaCreateManagedWidget("pageWidth", labelWidgetClass, box3,
  482.            NULL);
  483.  
  484.   pgWidth = XtVaCreateManagedWidget("pageWidthText",asciiTextWidgetClass, box3,
  485.            XtNeditType, XawtextEdit, NULL);
  486.  
  487.            XtVaCreateManagedWidget("pageHeight", labelWidgetClass, box3,
  488.            NULL);
  489.  
  490.   pgHeight = XtVaCreateManagedWidget("pageHeightText",asciiTextWidgetClass, box3,
  491.            XtNeditType, XawtextEdit, NULL);
  492.  
  493.            XtVaCreateManagedWidget("horizMargin", labelWidgetClass, box3,
  494.            NULL);
  495.  
  496.   horizMargin = XtVaCreateManagedWidget("horizMarginText",asciiTextWidgetClass, box3,
  497.            XtNeditType, XawtextEdit, NULL);
  498.  
  499.            XtVaCreateManagedWidget("vertMargin", labelWidgetClass, box3,
  500.            NULL);
  501.  
  502.   vertMargin = XtVaCreateManagedWidget("vertMarginText",asciiTextWidgetClass, box3,
  503.            XtNeditType, XawtextEdit, NULL);
  504.  
  505.  
  506.   /* image processing options */
  507.   box4 = XtVaCreateManagedWidget("box4", formWidgetClass, dialog, NULL);
  508.  
  509.            XtVaCreateManagedWidget("prOptions", labelWidgetClass, box4,
  510.            NULL);
  511.   cnvBtns= XtVaCreateManagedWidget("dither", toggleWidgetClass, box4,
  512.            XtNradioData, DITHER,
  513.        NULL);
  514.            XtVaCreateManagedWidget("mdither", toggleWidgetClass, box4,
  515.            XtNradioData, MDITHER,
  516.        XtNradioGroup, cnvBtns,
  517.        NULL);
  518.            XtVaCreateManagedWidget("halftone", toggleWidgetClass, box4,
  519.            XtNradioData, HALFTONE,
  520.        XtNradioGroup, cnvBtns,
  521.        NULL);
  522.            XtVaCreateManagedWidget("mapbw", toggleWidgetClass, box4,
  523.            XtNradioData, MAPBW,
  524.        XtNradioGroup, cnvBtns,
  525.        NULL);
  526.  
  527.            XtVaCreateManagedWidget("brightnessLbl", labelWidgetClass, box4,
  528.        NULL);
  529.   brtext = XtVaCreateManagedWidget("brightnessText", asciiTextWidgetClass, box4,
  530.            XtNeditType, XawtextEdit, NULL);
  531.  
  532.   revBtn = XtVaCreateManagedWidget("reverse", toggleWidgetClass, box4, NULL);
  533.  
  534.   borderBtn = XtVaCreateManagedWidget("borders", toggleWidgetClass, box4, NULL);
  535.  
  536.   box5 = XtVaCreateManagedWidget("box5", formWidgetClass, dialog, NULL);
  537.  
  538.            XtVaCreateManagedWidget("outputLbl", labelWidgetClass, box5,
  539.        NULL);
  540.  
  541.   outBtns= XtVaCreateManagedWidget("file", toggleWidgetClass, box5,
  542.            XtNradioData, TOFILE,
  543.        NULL);
  544.  
  545.   filenm = XtVaCreateManagedWidget("fileText", asciiTextWidgetClass, box5,
  546.            XtNeditType, XawtextEdit, NULL);
  547.  
  548.            XtVaCreateManagedWidget("printer", toggleWidgetClass, box5,
  549.            XtNradioData, TOPRN,
  550.        XtNradioGroup, outBtns,
  551.        NULL);
  552.  
  553.   prncmd = XtVaCreateManagedWidget("printerText", asciiTextWidgetClass, box5,
  554.            XtNeditType, XawtextEdit, NULL);
  555.  
  556.  
  557.   box6 = XtVaCreateManagedWidget("box6", formWidgetClass, dialog, NULL);
  558.  
  559.   XtVaCreateManagedWidget("splat", labelWidgetClass, box6, NULL);
  560.   
  561.   button = XtVaCreateManagedWidget("OK", commandWidgetClass, box6, NULL);
  562.   XtAddCallback(button, XtNcallback, doGrab, NULL);
  563.  
  564.   button = XtVaCreateManagedWidget("Dismiss", commandWidgetClass, box6, NULL);
  565.   XtAddCallback(button, XtNcallback, doDismiss, NULL);
  566.  
  567. }
  568.  
  569.  
  570.  
  571. int main(argc, argv, envp)
  572.   int argc;
  573.   char *argv[];
  574.   char *envp;
  575. {
  576.   puts("X-Windows Screen Grabber");
  577.   puts(Copyright);
  578.   puts("");
  579.  
  580.  
  581.   hShell = XtAppInitialize(&appContext, "XGrab", NULL, 0, &argc, argv,
  582.      fallback_resources, NULL, 0);
  583.  
  584.   hDisplay = XtDisplay(hShell);
  585.   createWindow();
  586.  
  587.   XtRealizeWidget(hShell);
  588.   XtAppMainLoop(appContext);
  589. }
  590.